package io.github.cchenxi.crpc.core.api;

import java.util.List;

import io.github.cchenxi.crpc.core.meta.InstanceMeta;
import io.github.cchenxi.crpc.core.meta.ServiceMeta;
import io.github.cchenxi.crpc.core.registry.ChangedListener;

/**
 * 注册中心
 * Date: 2024-03-17
 *
 * @author chenxi
 */
public interface RegistryCenter {
    // provider && consumer
    void start();

    void stop();

    // provider endpoint

    void register(ServiceMeta service, InstanceMeta instance);

    void unregister(ServiceMeta service, InstanceMeta instance);

    // consumer endpoint

    List<InstanceMeta> fetchAll(ServiceMeta service);

    void subscribe(ServiceMeta service, ChangedListener listener);

    class StaticRegistryCenter implements RegistryCenter {
        List<InstanceMeta> providers;

        public StaticRegistryCenter(List<InstanceMeta > providers) {
            this.providers = providers;
        }

        @Override
        public void start() {

        }

        @Override
        public void stop() {

        }

        @Override
        public void register(ServiceMeta service, InstanceMeta instance) {

        }

        @Override
        public void unregister(ServiceMeta service, InstanceMeta instance) {

        }

        @Override
        public List<InstanceMeta> fetchAll(ServiceMeta service) {
            return providers;
        }

        @Override
        public void subscribe(ServiceMeta service, ChangedListener listener) {

        }
    }
}
